/* 
	PROJECT: scsu-csc653-chatter
	ABOUT: chatter client class for starting communication with chatter server and getting/assigning input/output streams
	DATE: 09-2010
	AUTHORS: skenny214@gmail.com, jinhongli08@gmail.com, hourigan.brian@gmail.com, yaqiluo2010@gmail.com
*/

package chatter.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatterClient {
	public Socket socket;
	
	//create global properties for input output from the chatter server
	public ChatterClientInputStream clientInput;
	public ChatterClientOutputStream chatterOutput;
	
	//make the constructor
	public ChatterClient(){

	}
	
	//a login method that accepts the username and password. makes initial connection to server and gets input/output streams
	public void login(String username, String password, String host){
		try {
			//create a new socket
			if(ChatterClientApplet.isDebug) System.out.print("starting socket connection...");
			socket = new Socket(host, 4000);
			if(ChatterClientApplet.isDebug) System.out.println("finished.");
			
			//create the input steam object to accept messages from server (runs as own thread)
			clientInput = new ChatterClientInputStream(this.socket.getInputStream());
			//add a listener for input stream events from server. This listener is a javascript binding listener
			clientInput.addChatterClientInputListenter(new JavascriptChatterClientInputListener());
			clientInput.start();
			
			chatterOutput = new ChatterClientOutputStream(this.socket.getOutputStream());
			//as a convenience perform the login request
			chatterOutput.login(username, password);
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		
	}
	
	
}
